//
//  Array-cut.swift
//  Pods
//
//  Created by zhu xietong on 2017/8/12.
//
//

import Foundation

extension Array{
    public subscript(_ from:Int?,_ to:Int?) ->Array{
        var newList = [Element]()
        
        
        let f = from ?? 0
        let t = to ?? self.count
        for (i,obj) in self.enumerated()
        {
            if i >= f && i <= t
            {
                newList.append(obj)
            }
        }
        return newList
    }
}
extension Array {
    
    // 去重
    public func filterDuplicate()->[Element]
    {
        var result = [Element]()
        
        var keys = [String]()
        for one in self
        {
            if keys.contains("\(one)")
            {
                continue
            }
            else{
                keys.append("\(one)")
                result.append(one)
            }
        }
        return result
    }
    public func filterDuplicates<E: Equatable>(_ filter: (Element) -> E) -> [Element] {
        var result = [Element]()
        for value in self {
            let key = filter(value)
            if !result.map({filter($0)}).contains(key) {
                result.append(value)
            }
        }
        return result
        //        let arrays = ["1", "2", "2", "3", "4", "4"]
        //        let filterArrays = arrays.filterDuplicates({$0})
        //        print(filterArrays)
        //
        //        let modelArrays = [1,2,3,4,4,2]
        //        let filterModels = modelArrays.filterDuplicates({$0})
        //        print(filterModels)
    }
    
}





extension NSArray{
    public subscript(_ from:Int?,_ to:Int?) ->NSMutableArray{
        let f = from ?? 0
        let t = to ?? self.count

        let newList = NSMutableArray()
        for (i,obj) in self.enumerated()
        {
            if i >= f && i <= t
            {
                newList.add(obj)
            }
        }
        return newList
    }
}
